// Copyright 2009, Google Inc. All Rights Reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * This code sample retrieves the list of profiles of given account
 * in case if latter is specified.
 */

using System;
using System.Xml;
using System.Xml.XPath;

namespace AdminServiceGetProfileListREST
{
	public class GetProfileList
	{
		// Provide login information.
		private const string login = "YOUR_LOGIN";
		private const string password = "YOUR_PASSWORD";
		private const int accountId = 4;

		private const string serviceURL = "http://SERVER_NAME:URCHIN_PORT/services/v1/adminservice/profiles/";

		static void Main()
		{
			try
			{
				// Compose REST URL.
				string url = serviceURL + "?login=" + login + "&password=" + password + "&accountId=" + accountId;
				// Retrieve profile list.
				XPathDocument xPathDocument = new XPathDocument(url);
				// Process response XML.
				XPathNavigator xPathNavigator = xPathDocument.CreateNavigator();
				XmlNameTable xmlNameTable = new NameTable();
				XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlNameTable);
				xmlNamespaceManager.AddNamespace("tns", "https://urchin.com/api/urchin/v1/");
				// Get all profiles.
				XPathNodeIterator profileXPathNodeIterator = xPathNavigator.Select("/tns:getProfileListResponse/profile", xmlNamespaceManager);
				// Display profiles info.
				while (profileXPathNodeIterator.MoveNext())
					DisplayProfile(profileXPathNodeIterator.Current);
			}
			catch(Exception e)
			{
				Console.WriteLine(e);
			}
		}

		private static void DisplayProfile(XPathNavigator profileXPathNavigator)
		{
			Console.Write("Profile id is \"" + profileXPathNavigator.SelectSingleNode("profileId/text()") + "\", ");
			Console.Write("profile name is \"" + profileXPathNavigator.SelectSingleNode("profileName/text()") + "\", ");
			Console.Write("account id is \"" + profileXPathNavigator.SelectSingleNode("accountId/text()") + "\", ");
			Console.WriteLine("account name is \"" + profileXPathNavigator.SelectSingleNode("accountName/text()") + "\" ");
		}
	}
}
